package au.com.handsunited.web.session;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.util.WebUtils;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.User;

/**
 * All access to the session will be through this class.
 *
 * @author Arun Chandrasekhar
 */
public class SessionSupport {

    public static final String USER = "loggedInUser";
    public static final String ACTIVE_ENTITY = "activeEntity";


    /**
     *
     * @param request
     * @return
     */
    public static User getUser(HttpServletRequest request) {
    	return (User) WebUtils.getSessionAttribute(request, USER);
    }


    /**
     * Set the current logged in User.
     * @param the http request to look up the session from
     * @param user the user to set into the session
     */
    public static void setLoggedInUser(HttpServletRequest request, User user) {
        WebUtils.setSessionAttribute(request, USER, user);
        setCurrentlyActiveEntity(request, user);
    }


    public static BaseEntity getCurrentlyActiveEntity(HttpServletRequest request) {
    	return (BaseEntity) WebUtils.getSessionAttribute(request, ACTIVE_ENTITY);
    }


    public static void setCurrentlyActiveEntity(HttpServletRequest request, BaseEntity currentlyActiveEntity) {
    	WebUtils.setSessionAttribute(request, ACTIVE_ENTITY, currentlyActiveEntity);
    }

}
